package com.bjgov.scoresys.service;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.HibernateException;
import org.hibernate.ObjectNotFoundException;

import com.bjgov.scoresys.dao.ScoreItemDAO;
import com.bjgov.scoresys.entity.ScoreItem;
import com.bjgov.scoresys.util.Page;
import com.bjgov.scoresys.util.PageUtil;
import com.bjgov.scoresys.util.Result;

public class ScoreSubItemsHandler {

	private static final Log log = LogFactory.getLog(ScoreSubItemsHandler.class);	
	private static ScoreItemDAO dao = new ScoreItemDAO();
	
	public static void save(ScoreItem scoreItem)
	{
		dao.save(scoreItem);
	}
	
	public static ScoreItem loadByID(int id)
	{
		return dao.findById((Integer)id);
	}
	
	public static List<ScoreItem> getItemsByIds(String ids) {
		if(StringUtils.isEmpty(ids)) {
			return new ArrayList<ScoreItem>();
		}
		if(ids.endsWith(",")) ids = ids.substring(0, ids.length()-1);
		return dao.findByIds(ids);
	}
	
	public static Result list(Page page) throws HibernateException,
	ObjectNotFoundException {
		int totalRecords = dao.count();
		page = PageUtil.createPage(page, totalRecords);
		List list = dao.getByPage(page);
		return new Result(list, page);
	}	
}
